package cn.edu.ctbu.datajpa01.webapi;

import cn.edu.ctbu.datajpa01.core.PageUtils;
import cn.edu.ctbu.datajpa01.domain.Schedule;
import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.service.ScheduleService;
import cn.edu.ctbu.datajpa01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/webapi/schedule")
public class ScheduleRestController {

    @Autowired
    ScheduleService scheduleService;

    /**
     * 分页和参数查询
     * @param page
     * @param size
     * @param name
     * @return
     */
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                        @RequestParam(value = "size",defaultValue = "10") Integer size,
                        @RequestParam(value = "name",defaultValue = "") String name) {

        Page<Schedule> schedulePage;
        //参数为空，正常查询
        if (StringUtils.isEmpty(name)){
            Pageable pageable= PageRequest.of(page,size, Sort.by("id").descending());
            schedulePage=scheduleService.findAll(pageable);
        }

        //匹配姓名查询
        else  {
            Schedule schedule=new Schedule();
            schedule.setName(name);
            Pageable pageable=  PageRequest.of(0,3, Sort.by("id").ascending());
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Schedule> example=Example.of(schedule,matcher);
            schedulePage=scheduleService.findAll(example,pageable);
        }


        PageUtils pageUtils=new PageUtils(schedulePage.getContent(), schedulePage.getTotalElements());
        return pageUtils;
    }


    /**
     * 新增课程信息
     * @param schedule
     * @return
     */
    @PostMapping("/insert")
    public  Schedule insert(Schedule schedule){

        Schedule schedule1 = scheduleService.insert(schedule);
        return schedule1;

    }

    /**
     * 修改课程信息
     * @param schedule
     * @return
     */
    @PutMapping("/update")
    public  Schedule update(Schedule schedule){
        //读取旧数据
        schedule = scheduleService.update(schedule);
        return schedule;

    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Schedule get(@PathVariable Integer id){
        Schedule schedule=scheduleService.getById(id);
        return schedule;
    }


    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable  Integer id){

        scheduleService.delete(id);
    }


    @GetMapping("/get")
    public List<Schedule> findAll(){
        return scheduleService.findAll();
    }

}
